<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function groupBy(collection, iteratee) {
        return collection.reduce((result, value) => {
          console.log(value, typeof iteratee);
          // 根据迭代函数计算分组依据
          const key =
            typeof iteratee === "function" ? iteratee(value) : value[iteratee];
          console.log(key);

          // 如果结果对象中还没有该键，则创建一个空数组
          if (!result[key]) {
            result[key] = [];
          }

          // 将元素放入相应的组中
          result[key].push(value);

          return result;
        }, {});
      }

      const numbers = [6.1, 4.2, 6.3, 8, 1, 0.36, 26.5];

      const groupedByFloor = groupBy(numbers, Math.floor);

      console.log(groupedByFloor);
      // 输出结果
      // {
      //   '4': [4.2],
      //   '6': [6.1, 6.3]
      // }
    </script>
  </body>
</html>
